package com.fanbai.score.user.activity;

import android.widget.EditText;

import com.fanbai.score.R;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.SPUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.bean.User_Bean;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by acer on 2017/6/11.
 */

public class EditNickName_Activity extends BaseActivity{

    @BindView(R.id.userActivityNickName_nicknameEdit)
    EditText nicknameEdit;
    @OnClick(R.id.userActivityNickName_submitBut)
    public void onClick(){
        if(!nicknameEdit.getText().toString().trim().equals("")){
            updatenickname(nicknameEdit.getText().toString().trim());
        }
    }

    @Override
    protected int getLayoutID() {
        return R.layout.user_activity_edit_nickname;
    }

    @Override
    public String getTitleName() {
        return "修改昵称";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }

    private void updatenickname(final String nickname){
        showDialog();
        UserRequestNetwork_Utils.updatenickname(ctx, nickname, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                hideDialog();
                User_Bean bean = JsonUtils.toBean(SPUtils.with(ctx).get(SPUtils.KEY_USER_USERINFO, ""), User_Bean.class);
                if(bean != null){
                    bean.setCnickname(nickname);
                }
                ToastUtils.RightImageToast(ctx, msg);
                ActivityManager.getInstance().finishActivity();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
